package com.ibm.mall.action;

import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.ibm.mall.base.BaseAction;
import com.ibm.mall.model.Appraisal;
import com.ibm.mall.model.Commodity;
import com.ibm.mall.model.CommodityClass;
import com.ibm.mall.model.PageBean;
import com.ibm.mall.util.QueryHelper;
import com.opensymphony.xwork2.ActionContext;

@Controller
@Scope("prototype")
public class CustomerCommodityAction extends BaseAction<Commodity> {

	private static final long serialVersionUID = 1L;
	
	private Integer pageNum;
	
	private Integer commodityClassId;
	
	private String commName;
	
	private Integer commodityId;
	
	public String productList() {
		if(pageNum==null){
			pageNum=1;
		}
		int pageSize=15;
		CommodityClass commodityClass=null;
		if(commodityClassId!=null&&!"".equals(commodityClassId)){
			ActionContext.getContext().put("commodityClassId", commodityClassId);
			commodityClass=commodityClassService.getCommdityClassById(commodityClassId);
		}
		QueryHelper queryHelper=new QueryHelper(Commodity.class, "c");
		if(commodityClass!=null){
			queryHelper.addCondition("c.commodityClass=?", commodityClass);
		}
		if(commName!=null){
			ActionContext.getContext().put("commName", commName);
			queryHelper.addCondition("c.commodityName like ?", "%"+commName+"%");
		}
		queryHelper.addCondition("c.status=?", (short)0);
		PageBean productPageBean=commodityService.getPageBean(pageNum, pageSize, queryHelper);
		ActionContext.getContext().put("productPageBean", productPageBean);
		List<CommodityClass> commodityClassList=commodityClassService.getTopCommodityClass();
		ActionContext.getContext().put("commodityClassList", commodityClassList);
		return "showProduct";
	}
	
	public String productDetail() {
		Commodity commodity=commodityService.getCommodityById(commodityId);
		ActionContext.getContext().put("commodity", commodity);
		QueryHelper queryHelper=new QueryHelper(Commodity.class, "c")
			.addCondition("c.status=?", (short)0)
			.addOrderProperty("insertTime", false);
		PageBean commodityPage=commodityService.getPageBean(1, 8, queryHelper);
		ActionContext.getContext().put("commodityPage", commodityPage);
		List<Appraisal> appraisalList=appraisalService.getAllAppraisalByCommodityId(commodity);
		ActionContext.getContext().put("appraisalList", appraisalList);
		return "showProductDetail";
	}

	public Integer getCommodityClassId() {
		return commodityClassId;
	}

	public void setCommodityClassId(Integer commodityClassId) {
		this.commodityClassId = commodityClassId;
	}

	public String getCommName() {
		return commName;
	}

	public void setCommName(String commName) {
		this.commName = commName;
	}

	public Integer getPageNum() {
		return pageNum;
	}

	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}

	public Integer getCommodityId() {
		return commodityId;
	}

	public void setCommodityId(Integer commodityId) {
		this.commodityId = commodityId;
	}
	
}
